package github.kawaiior.yuanchuxiuzhen.common.entity.addon.model;

import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.vertex.IVertexBuilder;
import github.kawaiior.yuanchuxiuzhen.common.entity.addon.StoneGiantEntity;
import github.kawaiior.yuanchuxiuzhen.util.RenderUtil;
import net.minecraft.client.renderer.entity.model.EntityModel;
import net.minecraft.client.renderer.model.ModelRenderer;
import net.minecraft.util.math.MathHelper;

public class StoneGiantEntityModel extends EntityModel<StoneGiantEntity> {

    private final ModelRenderer rightleg;
    private final ModelRenderer leftleg;
    private final ModelRenderer body;
    private final ModelRenderer rightarm;
    private final ModelRenderer rightarm2;
    private final ModelRenderer rightarm3;
    private final ModelRenderer rightarm4;

    public StoneGiantEntityModel(){
        this.textureWidth = 148;
        this.textureHeight = 66;

        this.rightleg = new ModelRenderer(this);
        this.rightleg.setRotationPoint(-2.0F, 4.0F, 0.0F);
        addBox(this.rightleg, 62, 40, -11.0F, 6.0F, -6.0F, 12, 14, 12, 0.0F, false);
        addBox(this.rightleg, 116, 28, -8.0F, -2.0F, -4.0F, 8, 8, 8, 0.0F, false);
        this.leftleg = new ModelRenderer(this);
        this.leftleg.setRotationPoint(2.0F, 4.0F, 0.0F);
        addBox(this.leftleg, 62, 40, -1.0F, 6.0F, -6.0F, 12, 14, 12, 0.0F, false);
        addBox(this.leftleg, 116, 28, 0.0F, -2.0F, -4.0F, 8, 8, 8, 0.0F, false);
        this.body = new ModelRenderer(this);
        this.body.setRotationPoint(0.0F, 24.0F, 0.0F);
        addBox(this.body, 0, 48, -6.0F, -24.0F, -6.0F, 12, 6, 12, 0.0F, false);
        addBox(this.body, 0, 28, -8.0F, -30.0F, -7.0F, 16, 6, 14, 0.0F, false);
        addBox(this.body, 0, 0, -10.0F, -40.0F, -9.0F, 20, 10, 18, 0.0F, false);
        this.rightarm = new ModelRenderer(this);
        this.rightarm.setRotationPoint(-10.0F, -16.0F, 0.5F);
        RenderUtil.setRotationAngle(this.rightarm, 0.0F, 0.0F, 0.0873F);
        addBox(this.rightarm, 116, 0, -8.0F, 0.0F, -3.5F, 8, 20, 8, 0.0F, false);
        this.rightarm2 = new ModelRenderer(this);
        this.rightarm2.setRotationPoint(0.0F, 0.0F, 0.5F);
        RenderUtil.setRotationAngle(this.rightarm2, 0.0F, 0.0F, 0.1745F);
        this.rightarm.addChild(this.rightarm2);
        addBox(this.rightarm2, 76, 0, -10.0F, -2.0F, -5.0F, 10, 10, 10, 0.0F, false);
        this.rightarm3 = new ModelRenderer(this);
        this.rightarm3.setRotationPoint(10.0F, -16.0F, 0.5F);
        RenderUtil.setRotationAngle(this.rightarm3, 0.0F, 0.0F, -0.0873F);
        addBox(this.rightarm3, 117, 0, 0.0F, 0.0F, -3.5F, 8, 20, 8, 0.0F, false);
        this.rightarm4 = new ModelRenderer(this);
        this.rightarm4.setRotationPoint(0.0F, 0.0F, 0.5F);
        RenderUtil.setRotationAngle(this.rightarm4, 0.0F, 0.0F, -0.1745F);
        this.rightarm3.addChild(this.rightarm4);
        addBox(this.rightarm4, 76, 20, 0.0F, -2.0F, -5.0F, 10, 10, 10, 0.0F, false);
    }

    private void addBox(ModelRenderer mb, int tx, int ty, float x, float y, float z, float width, float height, float depth, float delta, boolean mirrorIn){
        ModelRenderer mr = new ModelRenderer(this);
        mb.addChild(mr);
        mr.setTextureOffset(tx, ty).addBox(x,y,z,width,height,depth,delta,mirrorIn);
    }

    @Override
    public void setRotationAngles(StoneGiantEntity entityIn, float limbSwing, float limbSwingAmount, float ageInTicks, float netHeadYaw, float headPitch) {
        this.rightleg.rotateAngleX = MathHelper.cos(limbSwing * 1.0F) * 1.0F * limbSwingAmount;
        this.rightarm.rotateAngleX = MathHelper.cos(limbSwing * 0.6662F + 3.1415927F) * limbSwingAmount;
        this.leftleg.rotateAngleX = MathHelper.cos(limbSwing * 1.0F) * -1.0F * limbSwingAmount;
        this.rightarm3.rotateAngleX = MathHelper.cos(limbSwing * 0.6662F) * limbSwingAmount;
    }

    @Override
    public void render(MatrixStack matrixStackIn, IVertexBuilder bufferIn, int packedLightIn, int packedOverlayIn, float red, float green, float blue, float alpha) {
        this.rightleg.render(matrixStackIn, bufferIn, packedLightIn, packedOverlayIn);
        this.leftleg.render(matrixStackIn, bufferIn, packedLightIn, packedOverlayIn);
        this.body.render(matrixStackIn, bufferIn, packedLightIn, packedOverlayIn);
        this.rightarm.render(matrixStackIn, bufferIn, packedLightIn, packedOverlayIn);
        this.rightarm3.render(matrixStackIn, bufferIn, packedLightIn, packedOverlayIn);
    }
}
